package com.yserp.project.erp.basic.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yserp.common.constant.Constants;
import com.yserp.common.exception.BusinessException;
import com.yserp.common.utils.mybatis.MybatisPlusUtil;
import com.yserp.common.utils.text.Convert;
import com.yserp.project.erp.basic.goods.domain.BasisGoods;
import com.yserp.project.erp.basic.goods.domain.BasisGoodsVo;
import com.yserp.project.erp.basic.goods.mapper.BasisGoodsMapper;
import com.yserp.project.erp.basic.goods.service.IBasisGoodsService;
import com.yserp.project.erp.store.stock.domain.Stock;
import com.yserp.project.erp.store.stock.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 商品信息Service业务层处理
 *
 * @author yserp
 * @date 2022-06-29
 */
@Service
public class BasisGoodsServiceImpl extends ServiceImpl<BasisGoodsMapper,BasisGoods> implements IBasisGoodsService {
    @Resource
    private BasisGoodsMapper basisGoodsMapper;
    @Autowired
    private IStockService stockService;

    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public BasisGoods selectBasisGoodsById(Long id) {
        return basisGoodsMapper.selectById(id);
    }

    /**
     * 查询商品信息列表
     *
     * @param basisGoods 商品信息
     * @return 商品信息
     */
    @Override
    public List<BasisGoods> selectBasisGoodsList(BasisGoods basisGoods) {
        QueryWrapper<BasisGoods> queryWrapper = new QueryWrapper<>();
        MybatisPlusUtil.notNullField(basisGoods,queryWrapper);
        return basisGoodsMapper.selectList(queryWrapper);
    }

    /**
     * 查询商品信息列表
     *
     * @param basisGoods 商品信息
     * @return 商品信息
     */
    @Override
    public List<BasisGoodsVo> selectBasisGoodsListVo(BasisGoods basisGoods) {
        return basisGoodsMapper.selectBasisGoodsListVo(basisGoods);
    }


    /**
     * 批量删除商品信息
     *
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBasisGoodsByIds(String ids) {
        int result = 0;
        Long[] goodsIds = Convert.toLongArray(ids);
        for (Long goodId : goodsIds ) {
            result = deleteBasisGoodsById(goodId);
            if(result<=0){
                break;
            }
        }
        return result;
    }

    /**
     * 删除商品信息信息
     *
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteBasisGoodsById(Long id) {

        //查询商品是否有库存;如有库存则商品信息不能删除
        Stock stock = new Stock();
        stock.setGoodsId(id);
        List<Stock> stocks = stockService.selectStockList(stock);
        if(!CollectionUtils.isEmpty(stocks)){
            stock = stocks.get(0);
            //BigDecimal lockNumber = stock.getLockNumber();
            BigDecimal stockNumber = stock.getStockNumber();
            String goodsName = stock.getGoodsName();
            if(stockNumber.compareTo(BigDecimal.ZERO)>0){
                throw new BusinessException(goodsName+"有库存，不允许删除");
            }
        }

        return basisGoodsMapper.deleteById(id);
    }

    @Override
    public String checkGoodsCodeUnique(String goodsCode) {
        String flag = Constants.NAME_UNIQUE;
        int count = basisGoodsMapper.selectCount(new QueryWrapper<BasisGoods>().eq("goods_code", goodsCode));
        //int count = basisGoodsMapper.checkGoodsCodeUnique(goodsCode);
        if (count > 0) {
            flag = Constants.NAME_NOT_UNIQUE;
        }
        return flag;
    }

    @Override
    public int selectGoodsCount(BasisGoods basisGoods) {
        int i = 0;
        List<BasisGoods> list = this.selectBasisGoodsList(basisGoods);
        if(!CollectionUtils.isEmpty(list)){
            i = list.size();
        }
        return i;
    }
}
